<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Charge extends Admin_Controller {

	function __construct ()
	{
		parent::__construct();
        $this->load->model('project_m');
        $this->load->model('userinto_m');
        $this->load->model('charge_m');
        $this->load->library('pagination');
		if(!$this->auth->is_super_admin()){
			redirect(site_url('user/login'));
		}
	}
	
	public function index($page=1)
	{
	    $key=!empty($this->input->get('project_name'))?$this->input->get('project_name'):'';
        $limit = 30;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['reuse_query_string'] = TRUE;
        $config['base_url'] = base_url('manage/charge/index/');
        $config['total_rows'] = $this->charge_m->count_charge_find($key);
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $start = ($page-1)*$limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links($key);
        $data['charges'] = $this->charge_m->get_charge_list($start,$limit,$key);
        $data['key']=$key;
		$this->load->view('charge/index',$data);
	}
	//新增
    public function add(){
        $data['projects'] = $this->project_m->get_all_project();
	    $this->load->view('charge/form',$data);
    }
    //更新信息
    public function update(){
        $post = $this->input->post();
        $charge_id = isset($post['charge_id']) ? intval($post['charge_id']) : false;
        if (!$charge_id) {
            $charge = array(
                'project_id' =>$post['project_id'],
                'charge_money' =>  $post['charge_money'],
                'charge_text' =>  $post['charge_text'],
                'charge_time'=> time(),
            );
            $result = $this->charge_m->add_charge($charge);
            if($result){
                //更新已收款
                $project=$this->project_m->get_one_project($post['project_id']);
                $money=$project['project_get_money']+$post['charge_money'];
                $this->project_m->update_project($post['project_id'],array('project_get_money'=>$money));
                show_message('新增成功',site_url(X_URL.'charge/index'),1);
            }else{
                show_message('新增失败',site_url(X_URL.'charge/index'),1);
            }
        } else {
            // 修改
            $charge = array(
                'project_id' =>$post['project_id'],
                'charge_money' =>  $post['charge_money'],
                'charge_text' =>  $post['charge_text'],
            );
            $result =$this->charge_m->update_charge($charge_id,$charge);
            if($result){
                show_message('修改成功',site_url(X_URL.'charge/index'),1);
            }else{
                show_message('修改失败',site_url(X_URL.'charge/index'),1);
            }
        }
    }
    public function edit($charge_id){
        if ($charge_id) {
            $charge = $this->charge_m->get_one_charge($charge_id);
        } else {
            show_message('参数错误',site_url(X_URL.'charge/index'),1);
        }
        $data['projects'] = $this->project_m->get_all_project();
        $data['charge'] = $charge;
        $this->load->view('charge/form',$data);
    }

    public function delete($charge_id){
        if($charge_id){
            $result = $this->charge_m->update_charge($charge_id,array('delete'=>1));
            if($result){
                show_message('删除成功',site_url(X_URL.'charge/index'),1);
            }else{
                show_message('删除失败',site_url(X_URL.'charge/index'),1);
            }
        }else{
            show_message('参数错误',site_url(X_URL.'charge/index'),1);
        }
    }


}